//
//  RegisterViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/3.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//***************************用户注册***************************
#import "RegisterViewController.h"
#import "SoftworeExplainViewController.h"
#import "AppDelegate.h"
@interface RegisterViewController ()<UITextFieldDelegate>
@property(nonatomic,copy)NSArray *dataArray;
@property(nonatomic,copy)UIView *backgroundView;//背景
@property(nonatomic,copy)UIImageView *manImgView;
@property(nonatomic,copy)UIImageView *womanImgView;
@property(nonatomic,assign)NSInteger sexSelected;//1男，2女，默认是1
@property(nonatomic,assign)NSInteger protocolSelected;///0未选中  1选中,默认是0
@property(nonatomic,copy)NSTimer *myTimer;
@property(nonatomic,copy)UIButton *getCodeBtn;//获取验证码
@property(nonatomic,assign)int myTime;

@property(nonatomic,assign)CGFloat man_y;
@property(nonatomic,assign)CGFloat woman_y;
@property(nonatomic,copy)UIButton *manBtn;
@property(nonatomic,copy)UIButton *womanBtn;

@property(nonatomic,copy)NSString *myCode;

@property(nonatomic,copy)UITextField *phoneTextField;
@property(nonatomic,copy)UITextField *codeTextField;
@property(nonatomic,copy)UITextField *passwordTextField;
//@property(nonatomic,assign)CGRect textFieldFrame;

@end
@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:[UIView new]];
    _myTime=60;
    [self initData];
    [self createNavigationController];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)initData
{
    _dataArray=@[@{@"name":@"手机号码",@"image":@"telephone.png",@"placehoder":@"请输入手机号码"},@{@"name":@"验证码",@"image":@"code.png",@"placehoder":@"请输入验证码"},@{@"name":@"密  码",@"image":@"password-3-2.png",@"placehoder":@"请输入密码"}];
}
-(void)createNavigationController
{
    self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
    //返回按钮
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(0, 0, 25, 25);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
    //标题
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 44)];
    lab.text=@"注册";
    lab.textColor=[UIColor whiteColor];
    lab.font=[UIFont fontWithName:@"Arial-BoldMT" size:22 * kAdjustParamater];
    lab.textAlignment=NSTextAlignmentCenter;
    self.navigationItem.titleView=lab;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
}
#pragma mark 返回上一页
-(void)btnClick:(UIButton *)sender
{
    self.navigationController.navigationBar.hidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI
{
    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20+44, self.view.frame.size.width, self.view.frame.size.height-20-44)];
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.showsVerticalScrollIndicator=NO;
    scrollView.contentSize=CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height*1.25);
    [self.view addSubview:scrollView];
    
    
    _backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-20-44)];
    [scrollView addSubview:_backgroundView];
    CGFloat btn_w=self.view.frame.size.width-30*2;
    CGFloat btn_h=40;
    _backgroundView.userInteractionEnabled = YES;
    // 为背景添加手势
    UITapGestureRecognizer *backgroundTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelEdit:)];
    [_backgroundView addGestureRecognizer:backgroundTapGes];
    for (int i=0; i<_dataArray.count; i++) {
        //手机号码
        UILabel *phoneLab=[[UILabel alloc]initWithFrame:CGRectMake(30, 5+(btn_h*2+5)*i, btn_w, btn_h)];
        phoneLab.text=_dataArray[i][@"name"];
        phoneLab.font=[UIFont systemFontOfSize:16];
        phoneLab.textColor=[UIColor blackColor];
        [_backgroundView addSubview:phoneLab];
        
        UITextField *phoneTextField=[[UITextField alloc]initWithFrame:CGRectMake(phoneLab.frame.origin.x, CGRectGetMaxY(phoneLab.frame), btn_w, btn_h)];
//        phoneTextField.delegate=self;
        phoneTextField.backgroundColor=[UIColor whiteColor];
        phoneTextField.font=[UIFont systemFontOfSize:15 * kAdjustParamater];
        phoneTextField.tintColor = kCyanColor;
        phoneTextField.tag=i+1;
        phoneTextField.delegate = self;
        UIView *phoneView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 55, btn_h)];
        //phoneView.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
        
        UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, phoneView.frame.size.width-5, phoneView.frame.size.height)];
        backgroundView.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
        [phoneView addSubview:backgroundView];
        
        
        
        UIImageView *phoneImgView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 2, 30, btn_h-2*2)];
        UIImage *phoneImg=[UIImage imageNamed:_dataArray[i][@"image"]];
        phoneImg=[phoneImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        phoneImgView.image=phoneImg;
        
        [phoneView addSubview:phoneImgView];
        phoneTextField.tag=100+i;
        phoneTextField.leftView=phoneView;
        phoneTextField.leftViewMode=UITextFieldViewModeAlways;
        phoneTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
        
        phoneTextField.placeholder=_dataArray[i][@"placehoder"];
        phoneTextField.backgroundColor=[UIColor whiteColor];
        phoneTextField.layer.borderColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1].CGColor;
        phoneTextField.layer.cornerRadius=5.0;
        phoneTextField.layer.borderWidth=1.0;
        [_backgroundView addSubview:phoneTextField];
        if (i==1) {
            UIView *codeBackgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 70, btn_h)];
            UIButton *getCodeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            getCodeBtn.backgroundColor=[UIColor colorWithRed:37/255.0 green:116/255.0 blue:181/255.0 alpha:1];
            getCodeBtn.frame=CGRectMake(0, 10/2.0, 60, btn_h-10);
            getCodeBtn.titleLabel.font=[UIFont systemFontOfSize:10];
            getCodeBtn.layer.borderColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1].CGColor;
            getCodeBtn.layer.cornerRadius=2.0;
            getCodeBtn.layer.borderWidth=1.0;
            [getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [getCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [getCodeBtn addTarget:self action:@selector(getCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [codeBackgroundView addSubview:getCodeBtn];
            phoneTextField.rightView=codeBackgroundView;
            phoneTextField.rightViewMode=UITextFieldViewModeAlways;
            _getCodeBtn=getCodeBtn;
            _codeTextField=phoneTextField;
            
            
            phoneTextField.keyboardType=UIKeyboardTypeNumberPad;
            phoneImgView.frame=CGRectMake(10, 4, 30, btn_h-2*2-4);
            
        }else if (i==0){
            phoneTextField.keyboardType=UIKeyboardTypeNumberPad;
            phoneImgView.frame=CGRectMake(10, 4, 30, btn_h-2*2-4);
            _phoneTextField=phoneTextField;
        }else if (i==2){
            phoneTextField.secureTextEntry=YES;
            phoneImgView.frame=CGRectMake(10, 4, 30, btn_h-2*2-4);
            _passwordTextField=phoneTextField;
        }
    }
    //性别
    UILabel *nameLab=[[UILabel alloc]initWithFrame:CGRectMake(30, 15+(btn_h*2+5)*_dataArray.count, 50, btn_h)];
    nameLab.text=@"性  别";
    nameLab.font=[UIFont systemFontOfSize:15];
    nameLab.textColor=[UIColor blackColor];
    [_backgroundView addSubview:nameLab];
    
    NSArray *sexArray=@[@{@"sex":@"男",@"image":@"myyuan_selected"},@{@"sex":@"女",@"image":@"myyuan_normal"}];
    for (int i=0; i<sexArray.count; i++) {
        UIButton *sexBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [sexBtn setBackgroundImage:[UIImage imageNamed:sexArray[i][@"image"]] forState:UIControlStateNormal];
        sexBtn.frame=CGRectMake(CGRectGetMaxX(nameLab.frame)+10+(18+18+5)*i, nameLab.frame.origin.y+(btn_h-18)/2.0, 18, 18);
        [_backgroundView addSubview:sexBtn];
        
        
//        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nameLab.frame)+10+(20+20+5)*i, nameLab.frame.origin.y+(btn_h-20)/2.0, 22, 22)];
//        imgView.image=[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:sexArray[i][@"image"] ofType:nil]];
//        imgView.userInteractionEnabled=YES;
//        imgView.tag=i+1;
//        [_backgroundView addSubview:imgView];
        if (i==1) {
            sexBtn.frame=CGRectMake(CGRectGetMaxX(nameLab.frame)+10+(18+18+5)*i+15, nameLab.frame.origin.y+(btn_h-18)/2.0, 18, 18);
             _woman_y=sexBtn.frame.origin.y;
            _womanBtn=sexBtn;
            [sexBtn addTarget:self action:@selector(womanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        if(i==0){
            [sexBtn addTarget:self action:@selector(manBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            _man_y=sexBtn.frame.origin.y;
            _manBtn=sexBtn;
        }
//        
//        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHappen:)];
//        [imgView addGestureRecognizer:tap];
        
        UILabel *sexLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(sexBtn.frame)+3, nameLab.frame.origin.y, 20, btn_h)];
        sexLab.text=sexArray[i][@"sex"];
        sexLab.font=[UIFont systemFontOfSize:15];
        sexLab.textColor=[UIColor blackColor];
        [_backgroundView addSubview:sexLab];
    }
    _sexSelected=1;
    
    
    //注册
    UIButton *registerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    registerBtn.frame=CGRectMake((self.view.frame.size.width-150)/2.0, CGRectGetMaxY(nameLab.frame)+20, 150, 35);
    registerBtn.backgroundColor=[UIColor colorWithRed:89/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:20 * kAdjustParamater];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(registerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    registerBtn.layer.borderColor=[UIColor colorWithRed:89/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
    registerBtn.layer.borderWidth=1.0;
    registerBtn.layer.cornerRadius=5.0;
    [_backgroundView addSubview:registerBtn];
    
    
    //阅读用户协议
    UILabel *protocolLab=[[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-80)/2.0, CGRectGetMaxY(registerBtn.frame)+5, 80, 30)];
    protocolLab.text=@"阅读用户协议";
    //protocolLab.backgroundColor=[UIColor cyanColor];
    protocolLab.textAlignment=NSTextAlignmentCenter;
    protocolLab.font=[UIFont systemFontOfSize:12];
    protocolLab.textColor=[UIColor grayColor];
    [_backgroundView addSubview:protocolLab];
    
    UIImageView *protocolImgView=[[UIImageView alloc]initWithFrame:CGRectMake(protocolLab.frame.origin.x-20, protocolLab.frame.origin.y+(30-18)/2.0, 18, 18)];
    protocolImgView.image=[UIImage imageNamed:@"protocol_selected.png"];
    [_backgroundView addSubview:protocolImgView];
    
    protocolImgView.tag=3;
    protocolImgView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHappen:)];
    [protocolImgView addGestureRecognizer:tap];
    _protocolSelected=1;
    
    
    UIButton *protocolBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    protocolBtn.frame=CGRectMake(protocolLab.frame.origin.x, protocolLab.frame.origin.y, protocolLab.frame.size.width, protocolImgView.frame.size.height);
    [protocolBtn addTarget:self action:@selector(protocolBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_backgroundView addSubview:protocolBtn];
    
}
//阅读用户协议
-(void)protocolBtnClick:(UIButton *)sender
{
    NSLog(@"阅读用户协议");
    SoftworeExplainViewController *vc=[SoftworeExplainViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 点击空白处取消textfield
- (void)cancelEdit:(UITapGestureRecognizer *)tapGes
{
    [self.view endEditing:YES];
}

#pragma mark - textField代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark 获取验证码
-(void)getCodeBtnClick:(UIButton *)sender
{
   
    
    UITextField *textField=[self.view viewWithTag:100];
    if ([self checkTel:textField.text]) {
        
        [_phoneTextField resignFirstResponder];
        [_codeTextField becomeFirstResponder];
            _myTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
            _getCodeBtn.userInteractionEnabled=NO;
            [self getCodeData];
    }
   

}

#pragma mark 启动定时器
-(void)timerRun
{
    if (_myTime>=0) {
        [_getCodeBtn setTitle:[NSString stringWithFormat:@"%ds后获取",_myTime] forState:UIControlStateNormal];
    }else{
        _myTime=60;
        _getCodeBtn.userInteractionEnabled=YES;
        [_myTimer invalidate];
        _myTimer=nil;
        [_getCodeBtn setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
    }
    _myTime--;
}


#pragma mark 注册
-(void)registerBtnClick:(UIButton *)sender
{
    if (![_myCode isEqualToString:_codeTextField.text]) {
        // 验证码不正确
        [self addAleltWithContent:@"验证码输入错误"];
    } else {
        if ([self checkTel:_phoneTextField.text]&&[_myCode isEqualToString:_codeTextField.text]&&_passwordTextField.text.length>0) {
            if (_passwordTextField.text.length<6 || [_passwordTextField.text containsString:@" "]) {
                [self addAleltWithContent:@"密码长度需要大于等于6位且不能包含空格"];
            } else {
                [self getRegisterData];
            }
          
        } else {
           
            [self addAleltWithContent:@"内容不能为空"];
        }
    }
   
}

#pragma mark - 根据内容提示
- (void)addAleltWithContent:(NSString *)content
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:content preferredStyle:UIAlertControllerStyleAlert];
    // 修改标题颜色
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"提示"];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertVC setValue:title forKey:@"attributedTitle"];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:sureAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark manBtnClick
-(void)manBtnClick:(UIButton *)sender
{
    NSLog(@"男");
    _sexSelected=1;
    //_manBtn.frame=CGRectMake(_manBtn.frame.origin.x, _manBtn.frame.origin.y, 22, 22);
    [_manBtn setBackgroundImage:[UIImage imageNamed:@"myyuan_selected"] forState:UIControlStateNormal];
   // _womanBtn.frame=CGRectMake(_womanBtn.frame.origin.x, _womanBtn.frame.origin.y, 22, 22);
    [_womanBtn setBackgroundImage:[UIImage imageNamed:@"myyuan_normal"] forState:UIControlStateNormal];
}
#pragma mark womanBtnClick
-(void)womanBtnClick:(UIButton *)sender
{
    NSLog(@"女");
    _sexSelected=2;
    //_manBtn.frame=CGRectMake(_manBtn.frame.origin.x, _womanBtn.frame.origin.y, 22, 22);
    [_manBtn setBackgroundImage:[UIImage imageNamed:@"myyuan_normal"] forState:UIControlStateNormal];
    //_womanBtn.frame=CGRectMake(_womanBtn.frame.origin.x, _manBtn.frame.origin.y, 22, 22);
    [_womanBtn setBackgroundImage:[UIImage imageNamed:@"myyuan_selected"] forState:UIControlStateNormal];
}

#pragma mark 响应手势
-(void)tapHappen:(UITapGestureRecognizer *)tap
{
    UIImageView *manImgView=[_backgroundView viewWithTag:1];
    UIImageView *womanImgView=[_backgroundView viewWithTag:2];
    UIImageView *protocolImgView=[_backgroundView viewWithTag:3];//阅读用户协议
    UIImageView *imgView=(id)tap.view;
    _sexSelected=imgView.tag;
   // NSLog(@"你选择了%ld",_sexSelected);
    if (imgView.tag==1) {
        manImgView.image=[UIImage imageNamed:@"app_sex_radio_1.png"];
        womanImgView.image=[UIImage imageNamed:@"app_sex_radio_2.png"];
    }else if (imgView.tag==2){
        manImgView.image=[UIImage imageNamed:@"app_sex_radio_2.png"];
        womanImgView.image=[UIImage imageNamed:@"app_sex_radio_1.png"];
    }else if (imgView.tag==3){
        if (_protocolSelected==0) {
            protocolImgView.image=[UIImage imageNamed:@"protocol_selected.png"];
            _protocolSelected=1;
        }else if (_protocolSelected==1){
            protocolImgView.image=[UIImage imageNamed:@"protocol_normal.png"];
            _protocolSelected=0;
        }
        
    }
}

#pragma mark 判断手机号码的正则表达式
- (BOOL)checkTel:(NSString *)str

{
    
    if ([str length] == 0) {
        
    //    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号码不能为空" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
      //  [alert show];
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle]addNoDismissAlertWithTitle:@"警告" AndContent:@"手机号码不能为空" toView:self];
        return NO;
        
    }
    
    //1[0-9]{10}
    
    //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
    
    //    NSString *regex = @"[0-9]{11}";
    
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]|(17[0-9])))\\d{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if (!isMatch) {
        
//        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号码格式不正确" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        
//        [alert show];
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle]addNoDismissAlertWithTitle:@"警告" AndContent:@"手机号码格式不正确" toView:self];
        return NO;
        
    }
    
    
    
    return YES;
    
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=NO;
    if (_myTime>0) {
        [_myTimer invalidate];
        _myTimer=nil;
        _myTime=60;
        _getCodeBtn.userInteractionEnabled=YES;
    }
}




//获取验证码
-(void)getCodeData
{
    AFHTTPRequestOperationManager * manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSLog(@"手机号码====%@",_phoneTextField.text);
    [manager POST:[NSString stringWithFormat:GetRegisterCode,_phoneTextField.text] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"login==userInformation===%@",dic);
        if (dic) {
            _myCode=dic[@"code"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"==========%@",error.localizedDescription);
        
    }];
    
}

//注册
-(void)getRegisterData
{
    AFHTTPRequestOperationManager * manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [self.view isLoading:YES];
    [self.navigationController.view endEditing:YES];
    [manager POST:Register parameters:@{@"phone":_phoneTextField.text,@"name":@"1gene",@"password":_passwordTextField.text,@"code":_myCode} success:^(AFHTTPRequestOperation *operation, id responseObject) {
         [self.view isLoading:NO];
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"register==userInformation===%@",dic);
        
        NSDictionary *statusDic = [dic valueForKey:@"status"];
        NSInteger codeCount = [[statusDic valueForKey:@"code"] integerValue];
        if (codeCount == 1) {
            NSString *msgString = [statusDic valueForKey:@"msg"];
            if ([msgString isEqualToString:@"帐号已存在"]) {
                [self addAleltWithContent:@"该手机号已经注册过，请直接登陆"];
            }
        } else if (codeCount == 0) {
            // 说明注册成功
            NSLog(@"注册成功");
            if (dic) {
                NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
                [user setObject:dic[@"token"] forKey:@"token"];
                [user setObject:dic[@"user"][@"id"] forKey:@"userId"];
                [user setObject:dic[@"user"][@"name"] forKey:@"userName"];
                [user setObject:dic[@"user"][@"phone"] forKey:@"userPhone"];
                [user setObject:dic[@"user"][@"rong_token"] forKey:@"rong_token"];
                [user setObject:_passwordTextField.text forKey:@"userPassword"];
                [user synchronize];
                // 保存用户注册成功--用于提醒用户--张国文
                [[NetStatusHandle shareHandle] saveUserRegisterIsSuccess:@"YES"];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    AppDelegate *app=[UIApplication sharedApplication].delegate;
                    [app showMainViewController:1];
                });
            }
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [self.view isLoading:NO];
        NSLog(@"%@",error.localizedDescription);
        
    }];
    
}
//#pragma mark 关闭键盘
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [_backgroundView endEditing:YES];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
















